#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setCentralWidget(ui->splitter);
    initToolButtonMenu();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initToolButtonMenu()
{
    ui->toolButton->addAction(ui->actionSelAll);
    ui->toolButton->addAction(ui->actionSelNoAll);
    ui->toolButton->addAction(ui->actionSelReverse);


    ui->toolButtonSelAll->setDefaultAction(ui->actionSelAll);
    ui->toolButtonSelNoAll->setDefaultAction(ui->actionSelNoAll);
    ui->toolButtonSelReverse->setDefaultAction(ui->actionSelReverse);

    ui->toolButton->setDefaultAction(ui->actionListAddItem);

    ui->listWidget->setContextMenuPolicy(Qt::CustomContextMenu);

}


void MainWindow::on_toolBox_currentChanged(int index)
{
    ui->tabWidget->setCurrentIndex(index);
}

void MainWindow::on_tabWidget_currentChanged(int index)
{
    ui->toolBox->setCurrentIndex(index);
}

void MainWindow::on_actionSelAll_triggered()
{
    qDebug()<<"action sel all";
    int count = ui->listWidget->count();
    for (int i = 0; i < count; ++i) {
        ui->listWidget->item(i)->setCheckState(Qt::Checked);
    }
}

void MainWindow::on_actionListInit_triggered()
{
    ui->listWidget->clear();
    QListWidgetItem *item;
    QMap<int, QString> initList;
    initList.insert(1, "张一");
    initList.insert(2, "张二");
    initList.insert(3, "张三");
    QMap<int, QString>::const_iterator itr = initList.constBegin();
    while(itr != initList.constEnd()) {
        qDebug()<<itr.key()<<itr.value();
        item = new QListWidgetItem;
        item->setText(itr.value());
        item->setCheckState(Qt::Unchecked);
        item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled);
        ui->listWidget->addItem(item);

        itr++;
    }
    ui->listWidget->setEditTriggers(QListWidget::DoubleClicked);
}


void MainWindow::on_listWidget_itemPressed(QListWidgetItem *item)
{
    ui->lineEdit->setText(item->text());
}

void MainWindow::on_listWidget_itemChanged(QListWidgetItem *item)
{
    ui->lineEdit->setText(item->text());
    qDebug()<< item->checkState();
}


void MainWindow::on_toolButtonSelNoAll_triggered(QAction *arg1)
{
    int count = ui->listWidget->count();
    for (int i = 0; i < count; ++i) {
        ui->listWidget->item(i)->setCheckState(Qt::Unchecked);
    }
}

void MainWindow::on_toolButtonSelReverse_triggered(QAction *arg1)
{
    int count = ui->listWidget->count();
    for (int i = 0; i < count; ++i) {
        QListWidgetItem *item =  ui->listWidget->item(i);
        if (item->checkState()) {
            item->setCheckState(Qt::Unchecked);
        }else {
            item->setCheckState(Qt::Checked);
        }
    }
}

void MainWindow::on_actionListClear_triggered()
{
    ui->listWidget->clear();
}

void MainWindow::on_actionListAddItem_triggered()
{
    addDialog = new ListAddDialog;
    connect(addDialog, &ListAddDialog::sendAddListItemText, this, &MainWindow::addListItem);
    addDialog->show();
}

void MainWindow::addListItem(QString data)
{
    QListWidgetItem *item;
    item = new QListWidgetItem;
    item->setText(data);
    item->setCheckState(Qt::Unchecked);
    item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled);
    ui->listWidget->addItem(item);
}

void MainWindow::insertListItem(QString data)
{
    const int curRowIndex = ui->listWidget->currentRow();
    QListWidgetItem *item = new QListWidgetItem;
    item->setText(data);
    item->setCheckState(Qt::Unchecked);
    item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled);
    ui->listWidget->insertItem(curRowIndex + 1, item);
}

void MainWindow::on_pushButton_clicked()
{
    addDialog = new ListAddDialog;
    connect(addDialog, &ListAddDialog::sendAddListItemText, this, &MainWindow::addListItem);
    addDialog->show();
}

void MainWindow::on_actionListInsertItem_triggered()
{
    qDebug()<<ui->listWidget->currentRow();
    addDialog = new ListAddDialog(nullptr, 1);
    connect(addDialog, &ListAddDialog::sendInsertListItemText, this, &MainWindow::insertListItem);
    addDialog->show();
}

void MainWindow::on_actionListDeleteItem_triggered()
{
    const int curRowIndex = ui->listWidget->currentRow();
    ui->listWidget->takeItem(curRowIndex);
}

void MainWindow::on_listWidget_customContextMenuRequested(const QPoint &pos)
{
    QMenu *menu = new QMenu(this);
    menu->addAction(ui->actionListInsertItem);
    menu->addAction(ui->actionListDeleteItem);
    menu->exec(QCursor::pos());
}
